﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CMS.DAL
{
    public class EPioneerVersionsDAL
    {
        /// <summary>
        /// 查询所有版本信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetEPioneerVersions()
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = "select top 200 * from ePioneer_Versions order by create_time desc";
                DataSet ds = SqlHelper.ExecuteGetDataSet(DBConn.EMPC, CommandType.Text, sql);
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];
            }
            catch
            {
                dt = null;
            }

            return dt;
        }

        /// <summary>
        /// 添加版本信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Entity.EPioneerVersionsModel model)
        {
            int count = 0;
            try
            {
                string sql = "insert into ePioneer_Versions(outver,clientver,[description],isPub,create_time) values(@Outver,@Clientver,@Detail,@IsPub,@Create_time)";
                SqlParameter[] values = {
                                            new SqlParameter("@Outver", model.outver),
                                            new SqlParameter("@Clientver", model.clientver),
                                            new SqlParameter("@Detail", model.description),
                                            new SqlParameter("@IsPub", model.isPub=="True"?"1":"0"),
                                            new SqlParameter("@Create_time", model.create_time)
                                        };
                count = SqlHelper.ExecuteNonQuery(DBConn.EMPC, CommandType.Text, sql, values);
            }
            catch
            {
                count = -1;
            }
            return count;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Entity.EPioneerVersionsModel model, string ispub)
        {
            int count = 0;
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("update dbo.ePioneer_Versions set isPub='" + ispub + "' ");
                if (string.IsNullOrEmpty(model.outver))
                {
                    sql.Append(" and outver='" + model.outver + "' ");
                }
                if (string.IsNullOrEmpty(model.clientver))
                {
                    sql.Append(" and clientver='" + model.clientver + "' ");
                }
                if (string.IsNullOrEmpty(model.description))
                {
                    sql.Append(" and [description]='" + model.description + "'");
                }
                sql.Append(" where id='" + model.id + "'");
                count = SqlHelper.ExecuteNonQuery(DBConn.EMPC, CommandType.Text, sql.ToString());
            }
            catch
            {
                count = -1;
            }
            return count;
        }

        /// <summary>
        /// 多条数据启用或取消
        /// </summary>
        /// <param name="list">ID列表</param>
        /// <param name="isPub">启用1，取消0</param>
        /// <returns></returns>
        public bool Update(string[] list, string isPub)
        {
            StringBuilder sqlStr = new StringBuilder();
            DateTime createDate = DateTime.Now;
            for (int i = 0; i < list.Length; i++)
            {
                sqlStr.Append(" update dbo.ePioneer_Versions set isPub='" + isPub + "' ");
                sqlStr.Append(" where id='" + list[i] + "' ");
            }
            int count = SqlHelper.ExecuteNonQuery(DBConn.EMPC, CommandType.Text, sqlStr.ToString());
            if (count > 0)
                return true;
            else
                return false;
        }
    }
}
